package cn.tedu.charging.order.controller;

import cn.tedu.charging.common.pojo.JsonResult;
import cn.tedu.charging.order.pojo.param.OrderAddParam;
import cn.tedu.charging.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/order")
@RestController
@Api(tags = "订单接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param orderAddParam
     * @return
     * GET http://localhost:8080/order/create/gunId=1&pileId=1&userId=1
     * POST http://localhost:8080/order/create/
     *
     * content-type:application-json
     * {
     *     userId:1,
     *     pileId:1,
     *     gunId:1
     * }
     */
    @ApiOperation("创建订单")
    @PostMapping("create")
    public JsonResult createOrder( @RequestBody OrderAddParam orderAddParam) {
        log.debug("订单创建入参:{}",orderAddParam);
        //创建类型的接口是否返回出参,要根据业务来决定,如果后续的业务需要返回id
        //做其他的操作,比如通过这个返回的id 去查看订单详情.返回的id是订单详情的入参
        log.debug("订单创键调用service入参:{}",orderAddParam);
        String orderNo = orderService.createOrder(orderAddParam);
        log.debug("订单创建入参:{},出参",orderAddParam,orderNo);
        JsonResult jsonResult = JsonResult.ok(orderNo, "订单创建成功");
        log.debug("订单创建入参:{},出参:{}",orderAddParam,jsonResult);
        //前端工程  正常 if(code == 2000) {alert("创建成功")}
        // 异常 if(code != 2000) {alert("创建成功")}
        return jsonResult;
    }
}
